-module(printf).
-compile(export_all).
-import(util, [pr/2]).

expected() ->
    test_output() ++ test_output().

test(Printf) ->
    Printf([""]),
    Printf([<<>>]),
    Printf(["Hello %d %s\n", 25, "World"]),
    Printf([<<"pi=%.3f\n">>, 3.141]).

test_output() ->
    [ "result=0",
      "result=0",
      "Hello 25 World",
      "result=15",
      "pi=3.141",
      "result=9" ].

load_library() ->
    case ffi:is_windows() of
        true -> ffi:load_library_c("msvcrt");
	false -> ffi:load_library_c("/lib/libc.so.6")
    end.

main() ->
    {ok, Lib} = load_library(),
    test(fun(Args) ->
                 Res = ffi:call_block(Lib, "printf", Args),
                 pr("result=~w", [Res])
         end),
    test(fun(Args) ->
                  Res = ffi:call(Lib, "printf", Args),
                  pr("result=~w", [Res])
          end),
    util:die().
